<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
    	$('input').keyup(function(event) {
    		/* Act on the event */
    		commitData();
    	});
    	$('select').change(function(){
    		var sel = $('select').val();
    		console.log('sel:'+sel)
    		if(sel=='是'){
	    		$('.pianli').show();
	    	}else if(sel == '否'){
	    		$('.pianli').hide();
	    	}
	    	commitData();
	    })

    })

    function commitData() {
    	var mark = $('#mark').val();
    	var xs = $('#xishu').val();
    	var sel = $('select').val()=='是'?true:false;
        var tr = $('tr');
        var fa = $('.fa').val();
        var fb = $('.fb').val();
        var map = {};
        for (var i = 1; i < tr.length; i++) {
            var name = tr.eq(i).find('td').eq(0).html();
            var value = tr.eq(i).find('td').eq(1).find('input').val();
            map[value] = name;

        }
        test(mark, sel, fa, fb, xs, map);
    }

    /**
    * 用户报价行为 用map集合 包裹，包括 公司或者组织名 + 报价具体数额，
    但是这里为了方便排序 选择以 报价数额为key 公司或者组织名为value
    如  var obj = {'100':'你好啊1','300':'你好啊3','200':'你好啊2'}; 
    *
    *
    */
    function test(mark, isPL, F1, F2, F3, map) {
        //报价
        //var map={'100.9':'公司1','300':'公司2','200':'公司3'};
        var a = [];
        $.each(map, function(key, val) { a[a.length] = key; });
        a.sort(sortNumber);
        //var list = bidPrice.split(",",bidNum);
        var markList = {};
        //list.sort(sortNumber);
        /*$.each(a,function(index,value){
        	alert(value);
        })*/
        var start = 0,
            end = a.length - 1;
        //是否需要计算偏离
        if (isPL) {
            if (a[0] > a[1] * (1 + F1) && a[end] < a[end - 1] * F2) {
                start = 1;
                end = end - 1;
            } else if (a[0] <= a[1] * (1 + F1) && a[end] < a[end - 1] * F2) {
                end = end - 1;
            } else if (a[0] > a[1] * (1 + F1) && a[end] >= a[end - 1] * F2) {
                start = 1;
            }
        }
        console.log(start, end);

        var Aavg = 0,//平均值
        	del = 0,//扣分数
            sum = 0;//各家公司报价总数
        for (var i = start; i <= end; i++) {
            sum += parseFloat(a[i]);
            console.log(sum);
        }
        Aavg = sum / (end - start + 1);
        console.log('sum :' + sum);
        console.log('Aavg :' + Aavg);
        console.log('F3 ' + F3);

        $.each(a, function(index, value) {
            console.log(index + ",  " + value + ",  " + Math.abs(value - Aavg) * 100 * F3 / Aavg);
            del = Math.abs(value - Aavg) * 100 * F3 / Aavg;
            var values = mark - del;

            console.log(values);
            if (values < 0) values = 0;
            else
                values = values.toFixed(2);
            markList[map[value]] = values;
            $('tr').eq(index+1).find('td').eq(2).text(Aavg.toFixed(2));
	        $('tr').eq(index+1).find('td').eq(3).text(del.toFixed(2));
	        $('tr').eq(index+1).find('td').eq(4).text(values);
	        $('tr').eq(index+1).find('td').eq(5).text(isPL);

        })

        console.log(markList);
    }

    function test1() {
        var map = {};
        map['500'] = 'key1';
        map['100'] = 'key2';
        map['3060.9'] = 'key3';
        var a = [];
        $.each(map, function(key, val) { a[a.length] = key; });
        a.sort(sortNumber);
        $.each(a, function(index, value) {
            alert(map[value]);
        })
    }

    function sortMap(a, b) {
        return 1
    }

    function sortNumber(a, b) {
        return b - a;
    }
    </script>
</head>

<body>
	<input type="text" name="mark" id="mark" value=""  placeholder="输入价格总分"/>
	<input type="text" name="xishu" id="xishu" value=""  placeholder="输入扣分系数"/>
	<select name="">
		<option value="" disabled="">请选择</option>
		<option value="否">否</option>
		<option value="是">是</option>
	</select>
	<div class="pianli" style="display: none;">
			<input type="text" class="fa"/><input type="text" class="fb"/>
		</div>
    <table>
    	<tr>
    		<th>公司名</th>
			<th>报价</th>
			<th>平均值</th>
			<th>扣分数</th>
			<th>最终得分</th>
			<th>备注(有无偏离值)</th>
		</tr>
        <tr>
            <td>甲</td>
            <td>
                <input>
            </td>
            <td></td>
			<td></td>
			<td></td>
			<td></td>
        </tr>
        <tr>
            <td>乙</td>
            <td>
                <input>
            </td>
        	<td></td>
			<td></td>
			<td></td>
			<td></td>
        </tr>
        <tr>
            <td>丁</td>
            <td>
                <input>
            </td>
            <td></td>
			<td></td>
			<td></td>
			<td></td>
        </tr>
    </table>
    <!-- <button onclick="commitData()">提交</button> -->
</body>

</html>